var ComputerBuilder = function (name) {
    var consoleHandler = function (msg, sourceDevice, targetDevice) {
        var d = new Date();
        var dateText =
            d.getFullYear() +
            "-" +
            d.getMonth() +
            1 +
            "-" +
            d.getDate() +
            " " +
            d.getHours() +
            ":" +
            d.getMinutes() +
            ":" +
            d.getSeconds();
        console.log(
            `${dateText}:[${targetDevice.name}] received device [${sourceDevice.name}] send message:\n-----\n${msg}\n-----\n`
        );
    };
    function Computer() {
        this.name = name;
        this.mac = "";
        this.ip = "";
        this.company = "";
        /**消息处理器 */
        this.messageHandlers = [];
        /**通过网线连接的设备 */
        this.connectDevices = [];
    }
    /**
     *
     * @param {*} data
     */
    Computer.prototype.sendMessage = function (data) {
        if (this.connectDevices.length === 0) {
            console.log(`${name}:please set connectDevices`);
            return -1;
        }
        if (this.connectDevices.length > 3) {
            console.log("最多只能连接3台设备");
            return;
        }
        this.connectDevices.forEach((device) => {
            if (data.targetMac !== this.mac) {
                device.receivedMessage(data, this);
            }
        });
        return 0;
    };
    /**
     *
     * @param {*} msg
     */
    Computer.prototype.receivedMessage = function (data, device) {
        if (this.connectDevices.length === 0) {
            console.log(`${name}:please set connectDevices`);
            return -1;
        }
        if (this.connectDevices.length > 3) {
            console.log("最多只能连接3台设备");
            return;
        }
        this.messageHandlers.forEach((handler) => {
            if (
                data.targetMac === this.mac ||
                data.targetMac === "00-00-00-00-00-00"
            ) {
                console.log("是发给我的数据，收到了");
            } else {
                console.info("不是发给我的数据，也收到了，嘿嘿嘿");
            }
            handler(data.msg, device, this);
        });
        return 0;
    };
    Computer.prototype.connectDevice = function (device) {
        this.connectDevices.push(device);
        if (
            device.connectDevices.find((d) => d.mac === this.mac) !== undefined
        ) {
            return;
        }
        device.connectDevice(this);
    };

    var computer = new Computer();
    console.log(`${name}:init...`);
    console.log(`${name}:register handlers...`);
    computer.messageHandlers.push(consoleHandler);
    console.log(`${name}:please set connectDevices`);
    return computer;
};

var HubBuilder = function (name) {
    /**
     * 转发
     * @param {*} msg
     * @param {*} sourceDevice
     * @param {*} targetDevice
     */
    var relayHandler = function (msg, sourceDevice, targetDevice) {
        targetDevice.connectDevices.forEach((device) => {
            device.receivedMessage(msg, sourceDevice);
        });
    };

    function Hub() {
        this.name = name;
        this.company = "";
        /**消息处理器 */
        this.messageHandlers = [];
        /**通过网线连接的设备 */
        this.connectDevices = [];
    }
    /**
     *
     * @param {*} msg
     */
    Hub.prototype.sendMessage = function (msg) {
        throw new Error("我是个Hub,不会发消息");
        return -1;
    };
    /**
     *
     * @param {*} msg
     */
    Hub.prototype.receivedMessage = function (msg, device) {
        if (this.connectDevices.length === 0) {
            console.log(`${name}:please set connectDevices`);
            return -1;
        }
        if (this.connectDevices.length > 10) {
            console.log("最多只能连接10台设备");
            return;
        }
        this.messageHandlers.forEach((handler) => {
            handler(msg, device, this);
        });
        return 0;
    };

    Hub.prototype.connectDevice = function (device) {
        this.connectDevices.push(device);
        if (
            device.connectDevices.find((d) => d.mac === this.mac) !== undefined
        ) {
            return;
        }
        device.connectDevice(this);
    };
    var hub = new Hub();
    hub.messageHandlers.push(relayHandler);
    return hub;
};

var ExchangeBuilder = function (name) {
    function Exchange() {
        this.name = name;
        this.mapTable = {};
    }
    Exchange.prototype.ConnectDevice = function (device) {
        this.mapTable;
    };
    return new Exchange();
};

var RouterBuilder = function (name) {
    function Router() {
        this.name = name;
        this.mapTable = {};
    }
    Router.prototype.ConnectDevice = function (device) {
        this.mapTable;
    };
    return new Router();
};

/**
 * 两台电脑的情况下，可以通过一根网线互联通信
 * 两台以上就没有办法了，毕竟一台电脑最多三个网卡，再多不合理了
 * 然后消息也是群发消息
 */
function case1() {
    var computerA = ComputerBuilder("PC A");
    computerA.mac = "0F-0A-0A-FF-FF-AA";
    var computerB = ComputerBuilder("PC B");
    computerB.mac = "0F-0A-0A-FF-FF-BB";
    var computerC = ComputerBuilder("PC C");
    computerC.mac = "0F-0A-0A-FF-FF-CC";
    var computerD = ComputerBuilder("PC D");
    computerD.mac = "0F-0A-0A-FF-FF-DD";
    var computerE = ComputerBuilder("PC E");
    computerE.mac = "0F-0A-0A-FF-FF-EE";

    computerA.connectDevice(computerB);
    computerA.connectDevice(computerC);
    computerA.connectDevice(computerD);
    // computerA.connectDevice(computerE);

    computerB.connectDevice(computerC);

    computerA.sendMessage({
        msg: "hi, I'm " + computerA.name,
        sourceMac: computerA.mac,
        targetMac: computerB.mac,
    });
    computerB.sendMessage({
        msg: "hi gays, I'm " + computerB.name,
        sourceMac: computerB.mac,
        targetMac: "00-00-00-00-00-00",
    });
}

function case2() {
    var computerA = ComputerBuilder("PC A");
    computerA.mac = "0F-0A-0A-FF-FF-AA";
    var computerB = ComputerBuilder("PC B");
    computerB.mac = "0F-0A-0A-FF-FF-BB";
    var computerC = ComputerBuilder("PC C");
    computerC.mac = "0F-0A-0A-FF-FF-CC";
    var computerD = ComputerBuilder("PC D");
    computerD.mac = "0F-0A-0A-FF-FF-DD";
    var computerE = ComputerBuilder("PC E");
    computerE.mac = "0F-0A-0A-FF-FF-EE";

    var hubA = HubBuilder("Hub A");
    hubA.connectDevice(computerA);
    hubA.connectDevice(computerB);
    hubA.connectDevice(computerC);
    hubA.connectDevice(computerD);
    hubA.connectDevice(computerE);

    computerA.sendMessage({
        msg: "hi, I'm " + computerA.name,
        sourceMac: computerA.mac,
        targetMac: computerB.mac,
    });
    computerB.sendMessage({
        msg: "hi gays, I'm " + computerB.name,
        sourceMac: computerB.mac,
        targetMac: "00-00-00-00-00-00",
    });
}
// case1();
case2();
